package com.example.mediamoudle.mediacodec;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baselibrary.base.BaseActivity;
import com.example.mediamoudle.R;
import com.example.mediamoudle.databinding.ActivityMediaCodecVideoBinding;
import com.example.mediamoudle.pickvideo.PickVideoActivity;
import com.example.mediamoudle.pickvideo.VideoFile;
import com.example.mediamoudle.router.MediaRouterPath;

import java.util.ArrayList;

/***
 * 解码视频播放
 */
@Route(path = MediaRouterPath.MEDIA_DECODE_VIDEO)
public class MediaCodecVideoActivity extends BaseActivity<ActivityMediaCodecVideoBinding> {
    @Override
    public int getLayoutId() {
        return  R.layout.activity_media_codec_video;
    }

    @Override
    public void initView() {

      mBinding.button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

            if (mBinding.button.getText().equals("播放")){
                MediaCodecHelper.INSTANCE.stop();
                mBinding.button.setText("暂停");
            }else {
                mBinding.button.setText("播放");
                MediaCodecHelper.INSTANCE.start();
            }

          }
      });
      mBinding.buttonChose.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivityForResult(new Intent(MediaCodecVideoActivity.this, PickVideoActivity.class), 1000);

          }
      });
    }

    @Override
    public void initListener() {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaCodecHelper.INSTANCE.stop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2000) {
           /* String path = data.getParcelableArrayListExtra("videoPath")

                    Log.i("TAG", "onActivityResult: path:$path")
            if (path != null) {
                initPlayer(path[0].path)
            }*/
            assert data != null;
            ArrayList<VideoFile> list =  data.getParcelableArrayListExtra("videoPath");
            Log.i("AFDS", "onActivityResult:list:$ "+list.get(0).getPath());
            MediaCodecHelper.INSTANCE.initMediaCodec(list.get(0).getPath(),mBinding.surface.getSurface());

        }
    }
}
